-
-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle svelte syntax #204
Handle svelte syntax #204
Conversation
src/printer.ts
Outdated
// Find single curly brackets for svelte | ||
const start2: number = text.indexOf('{'); | ||
if (this.options.pugFramework === 'svelte' && start2 !== -1) { | ||
result += text.slice(0, start2); | ||
text = text.slice(start2 + 1); | ||
const end2: number = text.indexOf('}'); | ||
if (end2 !== -1) { | ||
let code: string = text.slice(0, end2); | ||
try { | ||
// Index of primary quote | ||
const q1: number = code.indexOf(this.quotes); | ||
// Index of secondary (other) quote | ||
const q2: number = code.indexOf(this.otherQuotes); | ||
// Index of backtick | ||
const qb: number = code.indexOf('`'); | ||
if (q1 >= 0 && q2 >= 0 && q2 > q1 && (qb < 0 || q1 < qb)) { | ||
logger.log({ | ||
code, | ||
quotes: this.quotes, | ||
otherQuotes: this.otherQuotes, | ||
q1, | ||
q2, | ||
qb | ||
}); | ||
logger.warn( | ||
'The following expression could not be formatted correctly. Please try to fix it yourself and if there is a problem, please open a bug issue:', | ||
code | ||
); | ||
result += handleBracketSpacing(this.options.pugBracketSpacing, code); | ||
text = text.slice(end2 + 1); | ||
continue; | ||
} else { | ||
code = this.frameworkFormat(code); | ||
} | ||
} catch (error: unknown) { | ||
logger.warn('[PugPrinter:formatText]: ', error); | ||
try { | ||
code = format(code, { | ||
parser: 'babel', | ||
...this.codeInterpolationOptions, | ||
semi: false | ||
}); | ||
if (code[0] === ';') { | ||
code = code.slice(1); | ||
} | ||
} catch (error: unknown) { | ||
logger.warn(error); | ||
} | ||
} | ||
code = unwrapLineFeeds(code); | ||
result += handleBracketSpacing(this.options.pugBracketSpacing, code, ['{', '}']); | ||
text = text.slice(end2 + 1); | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section is just a stupid copy-paste of the above code, but it checks for one instead of two curly brackets...
We should refactor it and find out if there is a better solution.
Also the error handling was removed except a default warning and it's currently only checking if framework is set to svelte
We also need more test cases with text svelte interpolation. Currently I observed that the whole part in the try
block is failing and always the catch
fallback algorithm is taken 🤔.
Therefore can it removed for svelte, or are there possibilities where it's useful?
@GrygrFlzr @basaran @dummdidumm |
@@ -393,6 +394,7 @@ export class PugPrinter { | |||
private formatText(text: string): string { | |||
let result: string = ''; | |||
while (text) { | |||
// Find double curly brackets |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor this entire method into smaller parts (that can be reused and are easier to grasp)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, this code hurts, but I personally decided for now to await feedback from svelte community if there are further errors and printed warnings in their console output
Based on this feedback I can refactor this messy code
But now it's working and the svelte community can start working our formatter
Co-authored-by: ST-DDT <ST-DDT@gmx.de>
close #201
This also affected the behavior of
interpolation-token
tokens and it's bracket spacing handling